home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jdmaster.c < prev    next >
C/C++ Source or Header  |  1996-01-06  |  20KB  |  556 lines

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains master control logic for the JPEG decompressor.
  9.  * These routines are concerned with selecting the modules to be executed
  10.  * and with determining the number of passes and the work to be done in each
  11.  * pass.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef struct {
  22.   struct jpeg_decomp_master pub; /* public fields */
  23.  
  24.   int pass_number;        /* # of passes completed */
  25.  
  26.   boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
  27.  
  28.   /* Saved references to initialized quantizer modules,
  29.    * in case we need to switch modes.
  30.    */
  31.   struct jpeg_color_quantizer * quantizer_1pass;
  32.   struct jpeg_color_quantizer * quantizer_2pass;
  33. } my_decomp_master;
  34.  
  35. typedef my_decomp_master * my_master_ptr;
  36.  
  37.  
  38. /*
  39.  * Determine whether merged upsample/color conversion should be used.
  40.  * CRUCIAL: this must match the actual capabilities of jdmerge.c!
  41.  */
  42.  
  43. LOCAL(boolean)
  44. use_merged_upsample (j_decompress_ptr cinfo)
  45. {
  46. #ifdef UPSAMPLE_MERGING_SUPPORTED
  47.   /* Merging is the equivalent of plain box-filter upsampling */
  48.   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
  49.     return FALSE;
  50.   /* jdmerge.c only supports YCC=>RGB color conversion */
  51.   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
  52.       cinfo->out_color_space != JCS_RGB ||
  53.       cinfo->out_color_components != RGB_PIXELSIZE)
  54.     return FALSE;
  55.   /* and it only handles 2h1v or 2h2v sampling ratios */
  56.   if (cinfo->comp_info[0].h_samp_factor != 2 ||
  57.       cinfo->comp_info[1].h_samp_factor != 1 ||
  58.       cinfo->comp_info[2].h_samp_factor != 1 ||
  59.       cinfo->comp_info[0].v_samp_factor >  2 ||
  60.       cinfo->comp_info[1].v_samp_factor != 1 ||
  61.       cinfo->comp_info[2].v_samp_factor != 1)
  62.     return FALSE;
  63.   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  64.   if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  65.       cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  66.       cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
  67.     return FALSE;
  68.   /* ??? also need to test for upsample-time rescaling, when & if supported */
  69.   return TRUE;            /* by golly, it'll work... */
  70. #else
  71.   return FALSE;
  72. #endif
  73. }
  74.  
  75.  
  76. /*
  77.  * Compute output image dimensions and related values.
  78.  * NOTE: this is exported for possible use by application.
  79.  * Hence it mustn't do anything that can't be done twice.
  80.  * Also note that it may be called before the master module is initialized!
  81.  */
  82.  
  83. GLOBAL(void)
  84. jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
  85. /* Do computations that are needed before master selection phase */
  86. {
  87.   int ci;
  88.   jpeg_component_info *compptr;
  89.  
  90.   /* Prevent application from calling me at wrong times */
  91.   if (cinfo->global_state != DSTATE_READY)
  92.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  93.  
  94. #ifdef IDCT_SCALING_SUPPORTED
  95.  
  96.   /* Compute actual output image dimensions and DCT scaling choices. */
  97.   if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
  98.     /* Provide 1/8 scaling */
  99.     cinfo->output_width = (JDIMENSION)
  100.       jdiv_round_up((long) cinfo->image_width, 8L);
  101.     cinfo->output_height = (JDIMENSION)
  102.       jdiv_round_up((long) cinfo->image_height, 8L);
  103.     cinfo->min_DCT_scaled_size = 1;
  104.   } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
  105.     /* Provide 1/4 scaling */
  106.     cinfo->output_width = (JDIMENSION)
  107.       jdiv_round_up((long) cinfo->image_width, 4L);
  108.     cinfo->output_height = (JDIMENSION)
  109.       jdiv_round_up((long) cinfo->image_height, 4L);
  110.     cinfo->min_DCT_scaled_size = 2;
  111.   } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
  112.     /* Provide 1/2 scaling */
  113.     cinfo->output_width = (JDIMENSION)
  114.       jdiv_round_up((long) cinfo->image_width, 2L);
  115.     cinfo->output_height = (JDIMENSION)
  116.       jdiv_round_up((long) cinfo->image_height, 2L);
  117.     cinfo->min_DCT_scaled_size = 4;
  118.   } else {
  119.     /* Provide 1/1 scaling */
  120.     cinfo->output_width = cinfo->image_width;
  121.     cinfo->output_height = cinfo->image_height;
  122.     cinfo->min_DCT_scaled_size = DCTSIZE;
  123.   }
  124.   /* In selecting the actual DCT scaling for each component, we try to
  125.    * scale up the chroma components via IDCT scaling rather than upsampling.
  126.    * This saves time if the upsampler gets to use 1:1 scaling.
  127.    * Note this code assumes that the supported DCT scalings are powers of 2.
  128.    */
  129.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  130.        ci++, compptr++) {
  131.     int ssize = cinfo->min_DCT_scaled_size;
  132.     while (ssize < DCTSIZE &&
  133.        (compptr->h_samp_factor * ssize * 2 <=
  134.         cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
  135.        (compptr->v_samp_factor * ssize * 2 <=
  136.         cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
  137.       ssize = ssize * 2;
  138.     }
  139.     compptr->DCT_scaled_size = ssize;
  140.   }
  141.  
  142.   /* Recompute downsampled dimensions of components;
  143.    * application needs to know these if using raw downsampled data.
  144.    */
  145.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  146.        ci++, compptr++) {
  147.     /* Size in samples, after IDCT scaling */
  148.     compptr->downsampled_width = (JDIMENSION)
  149.       jdiv_round_up((long) cinfo->image_width *
  150.             (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
  151.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  152.     compptr->downsampled_height = (JDIMENSION)
  153.       jdiv_round_up((long) cinfo->image_height *
  154.             (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
  155.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  156.   }
  157.  
  158. #else /* !IDCT_SCALING_SUPPORTED */
  159.  
  160.   /* Hardwire it to "no scaling" */
  161.   cinfo->output_width = cinfo->image_width;
  162.   cinfo->output_height = cinfo->image_height;
  163.   /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
  164.    * and has computed unscaled downsampled_width and downsampled_height.
  165.    */
  166.  
  167. #endif /* IDCT_SCALING_SUPPORTED */
  168.  
  169.   /* Report number of components in selected colorspace. */
  170.   /* Probably this should be in the color conversion module... */
  171.   switch (cinfo->out_color_space) {
  172.   case JCS_GRAYSCALE:
  173.     cinfo->out_color_components = 1;
  174.     break;
  175.   case JCS_RGB:
  176. #if RGB_PIXELSIZE != 3
  177.     cinfo->out_color_components = RGB_PIXELSIZE;
  178.     break;
  179. #endif /* else share code with YCbCr */
  180.   case JCS_YCbCr:
  181.     cinfo->out_color_components = 3;
  182.     break;
  183.   case JCS_CMYK:
  184.   case JCS_YCCK:
  185.     cinfo->out_color_components = 4;
  186.     break;
  187.   default:            /* else must be same colorspace as in file */
  188.     cinfo->out_color_components = cinfo->num_components;
  189.     break;
  190.   }
  191.   cinfo->output_components = (cinfo->quantize_colors ? 1 :
  192.                   cinfo->out_color_components);
  193.  
  194.   /* See if upsampler will want to emit more than one row at a time */
  195.   if (use_merged_upsample(cinfo))
  196.     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  197.   else
  198.     cinfo->rec_outbuf_height = 1;
  199. }
  200.  
  201.  
  202. /*
  203.  * Several decompression processes need to range-limit values to the range
  204.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  205.  * due to noise introduced by quantization, roundoff error, etc.  These
  206.  * processes are inner loops and need to be as fast as possible.  On most
  207.  * machines, particularly CPUs with pipelines or instruction prefetch,
  208.  * a (subscript-check-less) C table lookup
  209.  *        x = sample_range_limit[x];
  210.  * is faster than explicit tests
  211.  *        if (x < 0)  x = 0;
  212.  *        else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  213.  * These processes all use a common table prepared by the routine below.
  214.  *
  215.  * For most steps we can mathematically guarantee that the initial value
  216.  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  217.  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  218.  * limiting step (just after the IDCT), a wildly out-of-range value is 
  219.  * possible if the input data is corrupt.  To avoid any chance of indexing
  220.  * off the end of memory and getting a bad-pointer trap, we perform the
  221.  * post-IDCT limiting thus:
  222.  *        x = range_limit[x & MASK];
  223.  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  224.  * samples.  Under normal circumstances this is more than enough range and
  225.  * a correct output will be generated; with bogus input data the mask will
  226.  * cause wraparound, and we will safely generate a bogus-but-in-range output.
  227.  * For the post-IDCT step, we want to convert the data from signed to unsigned
  228.  * representation by adding CENTERJSAMPLE at the same time that we limit it.
  229.  * So the post-IDCT limiting table ends up looking like this:
  230.  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  231.  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  232.  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  233.  *   0,1,...,CENTERJSAMPLE-1
  234.  * Negative inputs select values from the upper half of the table after
  235.  * masking.
  236.  *
  237.  * We can save some space by overlapping the start of the post-IDCT table
  238.  * with the simpler range limiting table.  The post-IDCT table begins at
  239.  * sample_range_limit + CENTERJSAMPLE.
  240.  *
  241.  * Note that the table is allocated in near data space on PCs; it's small
  242.  * enough and used often enough to justify this.
  243.  */
  244.  
  245. LOCAL(void)
  246. prepare_range_limit_table (j_decompress_ptr cinfo)
  247. /* Allocate and fill in the sample_range_limit table */
  248. {
  249.   JSAMPLE * table;
  250.   int i;
  251.  
  252.   table = (JSAMPLE *)
  253.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  254.         (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  255.   table += (MAXJSAMPLE+1);    /* allow negative subscripts of simple table */
  256.   cinfo->sample_range_limit = table;
  257.   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  258.   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  259.   /* Main part of "simple" table: limit[x] = x */
  260.   for (i = 0; i <= MAXJSAMPLE; i++)
  261.     table[i] = (JSAMPLE) i;
  262.   table += CENTERJSAMPLE;    /* Point to where post-IDCT table starts */
  263.   /* End of simple table, rest of first half of post-IDCT table */
  264.   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  265.     table[i] = MAXJSAMPLE;
  266.   /* Second half of post-IDCT table */
  267.   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  268.       (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  269.   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  270.       cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  271. }
  272.  
  273.  
  274. /*
  275.  * Master selection of decompression modules.
  276.  * This is done once at jpeg_start_decompress time.  We determine
  277.  * which modules will be used and give them appropriate initialization calls.
  278.  * We also initialize the decompressor input side to begin consuming data.
  279.  *
  280.  * Since jpeg_read_header has finished, we know what is in the SOF
  281.  * and (first) SOS markers.  We also have all the application parameter
  282.  * settings.
  283.  */
  284.  
  285. LOCAL(void)
  286. master_selection (j_decompress_ptr cinfo)
  287. {
  288.   my_master_ptr master = (my_master_ptr) cinfo->master;
  289.   boolean use_c_buffer;
  290.   long samplesperrow;
  291.   JDIMENSION jd_samplesperrow;
  292.  
  293.   /* Initialize dimensions and other stuff */
  294.   jpeg_calc_output_dimensions(cinfo);
  295.   prepare_range_limit_table(cinfo);
  296.  
  297.   /* Width of an output scanline must be representable as JDIMENSION. */
  298.   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  299.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  300.   if ((long) jd_samplesperrow != samplesperrow)
  301.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  302.  
  303.   /* Initialize my private state */
  304.   master->pass_number = 0;
  305.   master->using_merged_upsample = use_merged_upsample(cinfo);
  306.  
  307.   /* Color quantizer selection */
  308.   master->quantizer_1pass = NULL;
  309.   master->quantizer_2pass = NULL;
  310.   /* No mode changes if not using buffered-image mode. */
  311.   if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
  312.     cinfo->enable_1pass_quant = FALSE;
  313.     cinfo->enable_external_quant = FALSE;
  314.     cinfo->enable_2pass_quant = FALSE;
  315.   }
  316.   if (cinfo->quantize_colors) {
  317.     if (cinfo->raw_data_out)
  318.       ERREXIT(cinfo, JERR_NOTIMPL);
  319.     /* 2-pass quantizer only works in 3-component color space. */
  320.     if (cinfo->out_color_components != 3) {
  321.       cinfo->enable_1pass_quant = TRUE;
  322.       cinfo->enable_external_quant = FALSE;
  323.       cinfo->enable_2pass_quant = FALSE;
  324.       cinfo->colormap = NULL;
  325.     } else if (cinfo->colormap != NULL) {
  326.       cinfo->enable_external_quant = TRUE;
  327.     } else if (cinfo->two_pass_quantize) {
  328.       cinfo->enable_2pass_quant = TRUE;
  329.     } else {
  330.       cinfo->enable_1pass_quant = TRUE;
  331.     }
  332.  
  333.     if (cinfo->enable_1pass_quant) {
  334. #ifdef QUANT_1PASS_SUPPORTED
  335.       jinit_1pass_quantizer(cinfo);
  336.       master->quantizer_1pass = cinfo->cquantize;
  337. #else
  338.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  339. #endif
  340.     }
  341.  
  342.     /* We use the 2-pass code to map to external colormaps. */
  343.     if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  344. #ifdef QUANT_2PASS_SUPPORTED
  345.       jinit_2pass_quantizer(cinfo);
  346.       master->quantizer_2pass = cinfo->cquantize;
  347. #else
  348.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  349. #endif
  350.     }
  351.     /* If both quantizers are initialized, the 2-pass one is left active;
  352.      * this is necessary for starting with quantization to an external map.
  353.      */
  354.   }
  355.  
  356.   /* Post-processing: in particular, color conversion first */
  357.   if (! cinfo->raw_data_out) {
  358.     if (master->using_merged_upsample) {
  359. #ifdef UPSAMPLE_MERGING_SUPPORTED
  360.       jinit_merged_upsampler(cinfo); /* does color conversion too */
  361. #else
  362.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  363. #endif
  364.     } else {
  365.       jinit_color_deconverter(cinfo);
  366.       jinit_upsampler(cinfo);
  367.     }
  368.     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  369.   }
  370.   /* Inverse DCT */
  371.   jinit_inverse_dct(cinfo);
  372.   /* Entropy decoding: either Huffman or arithmetic coding. */
  373.   if (cinfo->arith_code) {
  374.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  375.   } else {
  376.     if (cinfo->progressive_mode) {
  377. #ifdef D_PROGRESSIVE_SUPPORTED
  378.       jinit_phuff_decoder(cinfo);
  379. #else
  380.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  381. #endif
  382.     } else
  383.       jinit_huff_decoder(cinfo);
  384.   }
  385.  
  386.   /* Initialize principal buffer controllers. */
  387.   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  388.   jinit_d_coef_controller(cinfo, use_c_buffer);
  389.  
  390.   if (! cinfo->raw_data_out)
  391.     jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  392.  
  393.   /* We can now tell the memory manager to allocate virtual arrays. */
  394.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  395.  
  396.   /* Initialize input side of decompressor to consume first scan. */
  397.   (*cinfo->inputctl->start_input_pass) (cinfo);
  398.  
  399. #ifdef D_MULTISCAN_FILES_SUPPORTED
  400.   /* If jpeg_start_decompress will read the whole file, initialize
  401.    * progress monitoring appropriately.  The input step is counted
  402.    * as one pass.
  403.    */
  404.   if (cinfo->progress != NULL && ! cinfo->buffered_image &&
  405.       cinfo->inputctl->has_multiple_scans) {
  406.     int nscans;
  407.     /* Estimate number of scans to set pass_limit. */
  408.     if (cinfo->progressive_mode) {
  409.       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  410.       nscans = 2 + 3 * cinfo->num_components;
  411.     } else {
  412.       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  413.       nscans = cinfo->num_components;
  414.     }
  415.     cinfo->progress->pass_counter = 0L;
  416.     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  417.     cinfo->progress->completed_passes = 0;
  418.     cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  419.     /* Count the input pass as done */
  420.     master->pass_number++;
  421.   }
  422. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  423. }
  424.  
  425.  
  426. /*
  427.  * Per-pass setup.
  428.  * This is called at the beginning of each output pass.  We determine which
  429.  * modules will be active during this pass and give them appropriate
  430.  * start_pass calls.  We also set is_dummy_pass to indicate whether this
  431.  * is a "real" output pass or a dummy pass for color quantization.
  432.  * (In the latter case, jdapi.c will crank the pass to completion.)
  433.  */
  434.  
  435. METHODDEF(void)
  436. prepare_for_output_pass (j_decompress_ptr cinfo)
  437. {
  438.   my_master_ptr master = (my_master_ptr) cinfo->master;
  439.  
  440.   if (master->pub.is_dummy_pass) {
  441. #ifdef QUANT_2PASS_SUPPORTED
  442.     /* Final pass of 2-pass quantization */
  443.     master->pub.is_dummy_pass = FALSE;
  444.     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  445.     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  446.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  447. #else
  448.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  449. #endif /* QUANT_2PASS_SUPPORTED */
  450.   } else {
  451.     if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  452.       /* Select new quantization method */
  453.       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  454.     cinfo->cquantize = master->quantizer_2pass;
  455.     master->pub.is_dummy_pass = TRUE;
  456.       } else if (cinfo->enable_1pass_quant) {
  457.     cinfo->cquantize = master->quantizer_1pass;
  458.       } else {
  459.     ERREXIT(cinfo, JERR_MODE_CHANGE);
  460.       }
  461.     }
  462.     (*cinfo->idct->start_pass) (cinfo);
  463.     (*cinfo->coef->start_output_pass) (cinfo);
  464.     if (! cinfo->raw_data_out) {
  465.       if (! master->using_merged_upsample)
  466.     (*cinfo->cconvert->start_pass) (cinfo);
  467.       (*cinfo->upsample->start_pass) (cinfo);
  468.       if (cinfo->quantize_colors)
  469.     (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  470.       (*cinfo->post->start_pass) (cinfo,
  471.         (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  472.       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  473.     }
  474.   }
  475.  
  476.   /* Set up progress monitor's pass info if present */
  477.   if (cinfo->progress != NULL) {
  478.     cinfo->progress->completed_passes = master->pass_number;
  479.     cinfo->progress->total_passes = master->pass_number +
  480.                     (master->pub.is_dummy_pass ? 2 : 1);
  481.     /* In buffered-image mode, we assume one more output pass if EOI not
  482.      * yet reached, but no more passes if EOI has been reached.
  483.      */
  484.     if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
  485.       cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  486.     }
  487.   }
  488. }
  489.  
  490.  
  491. /*
  492.  * Finish up at end of an output pass.
  493.  */
  494.  
  495. METHODDEF(void)
  496. finish_output_pass (j_decompress_ptr cinfo)
  497. {
  498.   my_master_ptr master = (my_master_ptr) cinfo->master;
  499.  
  500.   if (cinfo->quantize_colors)
  501.     (*cinfo->cquantize->finish_pass) (cinfo);
  502.   master->pass_number++;
  503. }
  504.  
  505.  
  506. #ifdef D_MULTISCAN_FILES_SUPPORTED
  507.  
  508. /*
  509.  * Switch to a new external colormap between output passes.
  510.  */
  511.  
  512. GLOBAL(void)
  513. jpeg_new_colormap (j_decompress_ptr cinfo)
  514. {
  515.   my_master_ptr master = (my_master_ptr) cinfo->master;
  516.  
  517.   /* Prevent application from calling me at wrong times */
  518.   if (cinfo->global_state != DSTATE_BUFIMAGE)
  519.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  520.  
  521.   if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  522.       cinfo->colormap != NULL) {
  523.     /* Select 2-pass quantizer for external colormap use */
  524.     cinfo->cquantize = master->quantizer_2pass;
  525.     /* Notify quantizer of colormap change */
  526.     (*cinfo->cquantize->new_color_map) (cinfo);
  527.     master->pub.is_dummy_pass = FALSE; /* just in case */
  528.   } else
  529.     ERREXIT(cinfo, JERR_MODE_CHANGE);
  530. }
  531.  
  532. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  533.  
  534.  
  535. /*
  536.  * Initialize master decompression control and select active modules.
  537.  * This is performed at the start of jpeg_start_decompress.
  538.  */
  539.  
  540. GLOBAL(void)
  541. jinit_master_decompress (j_decompress_ptr cinfo)
  542. {
  543.   my_master_ptr master;
  544.  
  545.   master = (my_master_ptr)
  546.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  547.                   SIZEOF(my_decomp_master));
  548.   cinfo->master = (struct jpeg_decomp_master *) master;
  549.   master->pub.prepare_for_output_pass = prepare_for_output_pass;
  550.   master->pub.finish_output_pass = finish_output_pass;
  551.  
  552.   master->pub.is_dummy_pass = FALSE;
  553.  
  554.   master_selection(cinfo);
  555. }
  556.